package com.sky.agent.service.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.sky.agent.config.OrgRouterDataConfig;
import com.sky.agent.service.ISysConfigService;
import com.sky.agent.dto.OrgRouterDto;
import com.sky.agent.dto.SquareDto;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class SysConfigServiceImpl implements ISysConfigService {

    @Value("${filePath.orgRouter}")
    private String filePathOrgRouter;
    @Value("${filePath.coffee}")
    private String filePathCoffee;
    @Resource
    private OrgRouterDataConfig orgRouterDataConfig;

    public Map orgRouter(OrgRouterDto orgRouterDto) {
        String coffeeStr = FileUtil.readString(this.filePathOrgRouter, StandardCharsets.UTF_8);
        Map map = (Map) JSONObject.parseObject(coffeeStr, Map.class);
        return map;
    }


    public Map getAppLstByOrganForSquare(SquareDto squareDto) {
        String coffeeStr = FileUtil.readString(this.filePathCoffee, StandardCharsets.UTF_8);
        Map map = (Map) JSONObject.parseObject(coffeeStr, Map.class);
        return map;
    }

//    @Override
//    public Map fileDel(String filePath) {
//        File file = new File(filePath);
//        scanFiles(file);
//        return null;
//    }

    //遍历一个目录下所有的文件
//    public static void scanFiles(File file) {
//        if (file == null || !file.exists()) {
//            return;
//        }
//        if (!file.isDirectory()) {
//
//            String[] fileNames =  file.getName().split("\\.");
//            if (StringUtils.isNumeric(fileNames[0]) && StringUtils.equals(fileNames[1], "pdf")) {
//                return;
//            }
//            if (!StringUtils.isNumeric(fileNames[0]) && StringUtils.startsWith(fileNames[0], "CCS_") && StringUtils.equals(fileNames[1], "xlsx")) {
//                return;
//            }
//            if (!StringUtils.isNumeric(fileNames[0]) && StringUtils.startsWith(fileNames[0], "INVOICE_") && StringUtils.equals(fileNames[1], "xlsx")) {
//                return;
//            }
//            System.out.println("删除的文件名：" + file.getName());
//            file.delete();
//        } else {
//            File[] files = file.listFiles();
//            if (files != null) {
//                for (File f : files) {
//                    scanFiles(f);
//                }
//            }
//        }
//    }
//    public static void main(String[] args) {
//        File file = new File("/Users/li.sun/Documents/li.sun/2022/2022-Non(1)");
//        scanFiles(file);
////        File file = new File("/Users/li.sun/Documents/szsk/社科通-app/IOS重构/兑换码/test_app_sn.txt");
////        List<String> list = FileUtil.readLines("/Users/li.sun/Documents/szsk/社科通-app/IOS重构/兑换码/test_app_sn.txt", StandardCharsets.UTF_8);
////
////        System.out.println(list.size());
////        String insertStr = null;
////        String[] strs = null;
////        // 环境，0：测试环境，1：正式环境
////        Integer profile = 1;
////        for (String str : list) {
////            strs = str.split(",");
////            insertStr = "INSERT INTO `sky_uum`.`umc_app_sn`(`app_id`, `sn`, `url`, `status`, `delete_status`, `add_time`, `use_time`, `del_time`, `profiles`) " +
////                    "VALUES ('a3ab6adb84e14dc7aee4fc046367206e', '"+strs[0]+"', '"+strs[1]+"', 0, 0, '"+ DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") +"', NULL, NULL, "+profile+");";
////            FileUtil.appendString(insertStr, new File("/Users/li.sun/Documents/szsk/社科通-app/IOS重构/兑换码/test_app_sn_2.txt"), StandardCharsets.UTF_8);
////        }
//    }
}

